<template>
  <div>
    <div>
      <el-input v-model="pages.name" placeholder="自习室名称" style="width: 200px"> </el-input>
      <el-button @click="search" icon="Search" style="margin-left: 10px;width: 100px" >查询</el-button>
      <el-button @click="handleAdd" icon="CirclePlus" style="margin-left: 10px;width: 150px" type="primary">新增自习室</el-button>
    </div>
    <el-table :data="tableData" style="width: 100%;margin-top: 20px" >
      <el-table-column label="自习室图">
        <template #default="scope">
          <img style="width: 100px" :src="genUrl(scope.row.pic)" />
        </template>
      </el-table-column>
      <el-table-column label="自习室名称" align="center">
        <template #default="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="位置" align="center">
        <template #default="scope">
        <span> {{ scope.row.local}}</span>
        </template>
      </el-table-column>
      <el-table-column label="可容纳数量" align="center">
        <template #default="scope">
          <el-tag >{{ scope.row.zwCount}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="可提前预约天数" align="center">
        <template #default="scope">
          <el-tag >{{ scope.row.prefixDay}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="360" align="center">
        <template #default="scope">
          <el-button size="small"  @click="handleEdit(scope.$index, scope.row)">
            编辑
          </el-button>  
        </template>
      </el-table-column>
    </el-table>

    <el-pagination layout="prev, pager, next,sizes" :default-current-page="pages.page" :total="pages.total" :page-sizes="[5, 20, 30, 40, 50, 100]" :page-size="pages.limit"

                   @size-change="handleSizeChange"
                   @current-change="handleCurrentChange"
    />

    <el-dialog v-if="dialogFormVisible" v-model="dialogFormVisible" title="" width="620">
      <el-form :model="form" label-width="150px">
        <el-form-item label="自习室图:" >
          <upload  v-if="dialogFormVisible" :limit="1" @change="change" :value="form.pic"></upload>
        </el-form-item>

        <el-form-item label="自习室名称:" >
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="位置:" >
          <el-input v-model="form.local"  />
        </el-form-item>
        <el-form-item label="可容纳数量:" >
          <el-input v-model="form.zwCount" />
        </el-form-item>
        <el-form-item label="可提前预约天数:" >
          <el-input v-model="form.prefixDay"  />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="upData">
            {{form.id != undefined ? '更新' : '新增'}}
          </el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script lang="ts" setup>
import {Timer, User} from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import Upload from "@/components/Upload.vue";
import { onBeforeMount, onMounted } from "vue";
import http  from "@/util/http-util";
import {ElMessage} from "element-plus";

interface Room {
  id: number
  name :string
  pic: string
  local: string
  zwCount:number
  prefixDay:number
}
const dialogFormVisible = ref(false)
const upData = () => {

  http.POST("/manage/addOrEditRoom",form.value).then((response) => {
    if (response.code == 0){
      ElMessage.success("更新成功")
      dialogFormVisible.value = false;
      loadData();
    }
  })

}

const handleSizeChange = (val: number) => {
  pages.value.limit = val;
  loadData();
}
const handleCurrentChange = (val: number) => {
  pages.value.page = val;
  loadData();
}


const search = ()=>{
  pages.value.page =1;
  loadData();
}

const handleAdd = () => {
  dialogFormVisible.value = true
  form.value = {
    id: undefined,
    name :undefined,
    pic: undefined,
    local: undefined,
    zwCount:undefined,
    prefixDay:3
  };
}

const handleEdit = (index: number, row: Room) => {
  console.log("row",row)
  form.value = {...row};
  console.log("row2",form.value)
  dialogFormVisible.value = true
}
const handleDelete = (index: number, row: Room) => {
  http.POST("/manage/deleteBanner",row).then((response) => {
    if (response.code == 0) {
      ElMessage.success("删除成功");
      loadData();
    }
  })
}
const change= (url)=>{
  form.value.pic = url;
}

const genUrl = (url)=>{
  return http.genUrl(url);
}

const tableData = ref<Room[]>([

])


const form = ref<any>({
  id: undefined,
  name :undefined,
  pic: undefined,
  local: undefined,
  zwCount:undefined,
  prefixDay:undefined
})

const pages = ref<any>({
  total:0,
  limit:5,
  page:1,
  name:undefined
});

const loadData = ()=>{
  console.log("ppp",pages.value)
  http.GET("/manage/getRoomList",pages.value).then((res)=>{
    tableData.value = res.data.records;
    pages.value.total =  res.data.total;
  })
}

onBeforeMount(()=>{
  loadData();
})
</script>